﻿using System;
using System.Web.UI.WebControls;
using BLL;
using Model;
using ZedGraph;
using System.Collections.Generic;

public partial class StatisticData_PackageandUnion_PackageQbyDT : System.Web.UI.Page
{
    private DateTime dt1
    {
        get { return DateTime.Parse(ViewState["starttime"].ToString()); }
        set { ViewState["starttime"] = value; }
    }
    private DateTime dt2
    {
        get { return DateTime.Parse(ViewState["stoptime"].ToString()); }
        set { ViewState["stoptime"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        dt1 = DateTime.Parse(this.TextBox1.Value.Trim());
        dt2 = DateTime.Parse(this.TextBox2.Value.Trim());
        BindPackageQ();
        this.ZedGraphWeb1.Visible = true;
    }

    protected void BindPackageQ()
    {        
        PackageProduct test = new PackageProduct();
        GridView1.DataSource = test.GetPackagedQbyAllPIDDT(dt1, dt2);
        GridView1.DataBind();        
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        BindPackageQ();
    }

    protected void ZedGraphWeb1_RenderGraph(ZedGraph.Web.ZedGraphWeb webObject, System.Drawing.Graphics g, ZedGraph.MasterPane pane)
    {
        int i = 0;
        double total = 0;
        PackageProduct tqt = new PackageProduct();
        List<Time_QInfo> t_qs = (List<Time_QInfo>)tqt.GetPackagedQbyAllPIDDT(dt1, dt2);

        GraphPane myPane = pane[0];

        myPane.Title.Text = "各产品类型包装量统计";

        //创建曲线
        double[] list = new double[t_qs.Count];
        string[] labels = new string[t_qs.Count];
        foreach (Time_QInfo t_q in t_qs)
        {
            list[i] = (double)t_q.Quantity;
            labels[i] = t_q.DayTime;
            i++;
        }

        for (int j = 0; j < t_qs.Count; j++)
        {
            total += list[j];
        }

        int count = 0;
        double other = 0;
        for (int j = 0; j < t_qs.Count; j++)
        {
            if (list[j] / total >= 0.05)
            {
                count++;
            }
            else
                other += list[j];
        }
        double[] list1 = new double[count + 1];
        string[] labels1 = new string[count + 1];
        int s = 0;
        for (int j = 0; j < t_qs.Count; j++)
        {
            if (list[j] / total >= 0.05)
            {
                list1[s] = list[j];
                labels1[s] = labels[j];
                s++;
            }
        }
        list1[count] = other;
        labels1[count] = "其它";

        
        PieItem[] pi = myPane.AddPieSlices(list1, labels1);
        foreach(PieItem p in pi)
        {
            MyGraphStyle.BuildPieItem(p);
            p.LabelDetail.FontSpec.Size = 10f;
        }

        MyGraphStyle.BuildpieGraphStyle(myPane);
        pane.AxisChange(g);
    }
}
